package com.snailhd.common.db.resultparsehander;

import com.alibaba.fastjson.JSONObject;
import com.snailhd.common.db.AbstractDBResultParseHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * @author hd
 * @description
 * @date 2019-08-17 22:13
 */
public class ObjectForDBResultParseHandler extends AbstractDBResultParseHandler {

    @Override
    public boolean supportsResult(Class clazz) {
        return true;
    }

    @Override
    public <T> T handle(ResultSet rs,Class<T> clazz, String field) throws SQLException {

        JSONObject obj = new JSONObject();
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();
        for(int i=1;i<=columnCount;i++){
            obj.put(metaData.getColumnName(i),rs.getObject(i));
        }
        T re = obj.toJavaObject(clazz);
        return re;
    }
}
